package com.cswangdao.service.impl;

import com.cswangdao.domain.po.Region;
import com.cswangdao.domain.vo.RegionVo;
import com.cswangdao.mapper.MarketRegionMapper;
import com.cswangdao.service.RegionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class RegionServiceImpl implements RegionService {


    @Autowired
    private MarketRegionMapper mapper;


    /**
     * @description: 查询地区信息
     * @param:
     * @return: com.cswangdao.domain.vo.RegionVo
     */
    @Override
    public  RegionVo list() {
        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession(true);)*/{
          //  MarketRegionMapper mapper = sqlSession.getMapper(MarketRegionMapper.class);
            List<Region> provinces = mapper.getRegionsByType((byte) 1);
            List<Region> cites = mapper.getRegionsByType((byte) 2);
            List<Region> areas = mapper.getRegionsByType((byte) 3);
            Map<Integer, List<Region>> c1 = cites.stream().collect(Collectors.groupingBy(Region::getPid));
            Map<Integer, List<Region>> c2 = areas.stream().collect(Collectors.groupingBy(Region::getPid));
            provinces.forEach(o->o.setChildren(c1.get(o.getId())));
            cites.forEach(o->o.setChildren(c2.get(o.getId())));
            RegionVo regionVo = RegionVo.builder()
                    .limit(31)
                    .page(1)
                    .pages(1)
                    .total(31)
                    .list(provinces)
                    .build();
            return regionVo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
